package org.aplombh.java.leetcode.all;

public class _20有效的括号 {
    public static void main(String[] args) {
        System.out.println(new Solution20_1().isValid("(])"));
    }
}


class Solution20_1 {

    char[] element = new char[10000 + 1];
    int t;
    char[] l = {'{', '(', '['};
    char[] r = {'}', ')', ']'};

    public boolean isValid(String s) {
        t = -1;

        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < 3; j++) {
                if (s.charAt(i) == l[j]) {
                    element[++t] = l[j];
                } else if (s.charAt(i) == r[j]) {
                    if (t == -1) return false;
                    if (element[t] == l[j])
                        t--;
                    else
                        return false;
                }
            }
        }
        return t == -1;
    }
}